<template>
  <div class="container">
    <!-- 上部搜索条件区域  面包屑-->
    <el-card class="filter-card">
      <div slot="header">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{path:'/home'}">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 数据筛选表单 -->
      <el-form ref="form" :model="form" label-width="40px" size="mini">
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <!-- label对应的data数据 匹配哪个选中 -->
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道">
          <el-select
           v-model="form.channel_id"
            placeholder="请选择频道"
             clearable
             @focus="getChanels"
            >
            <el-option
            v-for="item in channels"
            :label="item.name"
            :value="item.id"
            :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="form.date"
            type="datetimerange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="doSearch">查询</el-button>
        </el-form-item>
      </el-form>
      <!-- /数据筛选表单 -->
    </el-card>
    <!-- 下部文章列表区域 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">根据筛选条件共查询到{{total_count}} 条结果,当前是第{{form.page}}页：</div>
      <el-table :data="articles" style="width: 100%"  class="list-table" size="mini">
        <el-table-column prop="cover" label="封面">
          <template slot-scope="scope">
            <el-image :src="scope.row.cover.images[0]" style="width:150px;height:100px" lazy>
              <div slot="placeholder" class="image-slot">
                加载中
                <span class="dot">...</span>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
        <el-tag v-if="scope.row.status===0">草稿</el-tag>
        <el-tag v-else-if="scope.row.status===1" type="info">待审核</el-tag>
        <el-tag v-else-if="scope.row.status===2" type="success">审核通过</el-tag>
        <el-tag v-else-if="scope.row.status===3" type="warning">审核失败</el-tag>
        <el-tag v-else-if="scope.row.status===4" type="danger">已删除</el-tag>
    </template>
        </el-table-column>
        <el-table-column prop="pubdate" label="发布时间"></el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
        <el-button
           size="mini"
           icon="el-icon-edit"
           type="primary"
           circle
           @click="edit(scope.row.id)"></el-button>
        <el-button
           size="mini"
           type="danger"
           icon="el-icon-delete"
           circle
           v-if="scope.row.status===0"
           @click="del(scope.row.id)"></el-button>
    </template>
        </el-table-column>
      </el-table>
      <!-- 列表分页 -->
      <el-pagination
      style="margin-top:10px;"
      background
      layout="prev, pager, next"
      @current-change="hPageChange"
      :page-size="10"
      :total="total_count"
      :current-page="form.page"
     ></el-pagination>
      <!-- /列表分页 -->
    </el-card>
  </div>
</template>

<script>
import { getArticle, getArticleChannels, delArticle } from '@/api/article'
export default {
  name: 'ArticleIndex',
  data () {
    return {
      channels: [], // 频道列表
      total_count: 0, // 当前能查询的总条数
      articles: [], // 文章数据
      form: {
        status: null, // 查询文章的状态，默认为null
        channel_id: null, // 频道id
        page: 1, // 当前要查询的页码默认为1
        // per_page: 15, // 每页显示几条数据
        date: [] // 起始截止时间
      }
    }
  },
  methods: {
    loadChanels () {
      getArticleChannels().then(res => {
        this.channels = res.data.data.channels// 获取频道的数据
      })
    },
    loadArticles () {
      if (this.form.date === null) this.form.date = []
      if (this.form.channel_id === '') this.form.channel_id = null
      const parmaForm = { ...this.form }
      // const parmaForm = Object.assign({}, this.form)
      parmaForm.begin_pubdate = parmaForm.date[0]
      parmaForm.end_pubdate = parmaForm.date[1]
      delete parmaForm.date

      getArticle(
        parmaForm
      ).then(res => {
        this.articles = res.data.data.results
        this.total_count = res.data.data.total_count
      })
    },
    doSearch () {
      this.loadArticles()
      this.loadChanels()
      this.form.page = 1
    },
    hPageChange (page) {
      this.form.page = page
      this.loadArticles()
    },
    getChanels () {
      this.loadChanels()
    },
    del (id) {
      this.$confirm('确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delArticle(id.toString()).then(res => {
          this.loadArticles()
          this.$message.success('删除成功')
        })
      }).catch(() => {
        // 用户取消了
      })
    },
    edit (id, status) {
      this.$router.push({
        path: '/editArticle',
        query: {
          id,
          status
        }
      })
    }
  },
  mounted () {
    this.loadArticles()
  }
}
</script>

<style lang="less" scoped>
.container{
  .box-card{
    margin-top:20px ;
  }
}
</style>
